#!/usr/bin/env python3
# -*- coding: utf-8 -*-
""" HTTP客户端封装 """

import requests
from bs4 import BeautifulSoup
import org.rency.face.config.GlobalVariable as variable
import org.rency.face.config.Constants as const


class HttpClient:

    def __init__(self):
        self.config = variable.CONFIGURATION
        self.timeout = self.config.get(variable.CONF_HTTP_SECTION, variable.CONF_HTTP_TIMEOUT)
        self.userAgent = self.config.get(variable.CONF_HTTP_SECTION, variable.CONF_HTTP_USER_AGENT)
        self.logger = variable.LOGGER
        self.logger.getLogger().info('init http parameters, timeout:{}'.format(self.timeout))

    def get(self, url, param=None):
        headers = {const.DICT_HTTP_USER_AGENT: self.userAgent,
                   const.DICT_HTTP_ACCEPT_ENCODING: const.DICT_HTTP_ACCEPT_ENCODING_GZIP}

        self.logger.getLogger().debug('http request[get] {}'.format(url))

        response = requests.get(url, self.timeout, headers=headers)

        self.logger.getLogger().debug('http response[get] {}|{}'.format(response.status_code, url))

        if response.status_code == requests.codes.ok:
            soup = BeautifulSoup(response.text, "html.parser")
            print(soup.text)
        else:
            print('[{}], http request failed'.format(response.status_code))


if __name__ == '__main__':
    client = HttpClient()
    client.get('http://www.haosou.com/')
